package com.kingdee.pda.ui.inner_replenish;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.basecommon.utils.PreferencesUtils;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.adapter.InnerReplenishSkuListAdapter;
import com.kingdee.pda.callback.MyTouchLinstener;
import com.kingdee.pda.commonmethod.QueryStockPresenter;
import com.kingdee.pda.commonmethod.QueryStockView;
import com.kingdee.pda.entity.LocationListBean;
import com.kingdee.pda.entity.ReplenishCommitBean;
import com.kingdee.pda.entity.ReplenishOrderBean;
import com.kingdee.pda.entity.ReplenishOrderDetailBean;
import com.kingdee.pda.entity.ReplenishSkuBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.entity.WarehouseLocation;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.ui.activity.QueryLocationStockActivity;
import com.kingdee.pda.ui.popupwindow.ListSelectorPpWindow;
import com.kingdee.pda.ui.widget.CustomHScrollView;
import com.kingdee.pda.ui.widget.NoScrollListView;
import com.kingdee.pda.util.CommonUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

/**
 * 库内补货操作
 * Created by dlj
 * DATE 2021/6/24
 * TIME 11:25
 */
public class InnerReplenishActivity extends BaseTitleActivity implements View.OnFocusChangeListener, View.OnKeyListener,
        InnerReplenishContract.IInnerReplenishView, InnerReplenishSkuListAdapter.OnSkuItemClickListener, QueryStockView {

    private LinearLayout ll_replenish_order;
    private EditText et_replenishOrderNo;
    private EditText et_targetLocationcode;
    private EditText et_skuCode;
    private EditText et_fromLocationCode;
    private EditText et_replenish_count;

    private TextView tv_recommed_skuCode, tv_recommed_skuName, tv_replenish_detail, tv_recommed_skuItemCode;
    private TextView tv_total;

    private ImageView iv_order_select;
    private NoScrollListView mListView;
    private Button mBtnConfirm;
    private InnerReplenishSkuListAdapter mAdapter;
    private String billNo = "";
    private List<ReplenishOrderBean.DataBean> orderList = new ArrayList<>();
    //目标、来源库位信息（计算数量用）
    private WarehouseLocation targetWarehouseLocation;
    private WarehouseLocation fromWarehouseLocation;

    //后台数据分散，自己构建一个bean集合，保存补货的SKU信息(刷新下方sku列表用)
    private List<ReplenishSkuBean> replenishSkuBeans = new ArrayList<>();
    //正在操作的补货SKU对象
    private ReplenishSkuBean operatReplenishBean = null;
    private boolean isFocusToTargetLocation = true;
    private InnerReplenishPresenter innerReplenishPresenter;
    private int recommedPosition = 0;
    private boolean autoComplete;
    private CustomHScrollView customHScrollView;
    private QueryStockPresenter queryStockPresenter;
    private boolean isMoveOut;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, InnerReplenishActivity.class));
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_replenish);

        innerReplenishPresenter = new InnerReplenishPresenter(this, this);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("库内补货");
        ll_replenish_order = findViewById(R.id.ll_replenish_order);

        et_replenishOrderNo = findViewById(R.id.et_replenishOrderNo);
        et_targetLocationcode = findViewById(R.id.et_targetLocationcode);
        et_skuCode = findViewById(R.id.et_skuCode);
        et_fromLocationCode = findViewById(R.id.et_fromLocationCode);
        et_replenish_count = findViewById(R.id.et_replenish_count);

        tv_recommed_skuCode = findViewById(R.id.tv_recommed_skuCode);
        tv_recommed_skuName = findViewById(R.id.tv_recommed_skuName);
        tv_replenish_detail = findViewById(R.id.tv_replenish_detail);
        tv_total = findViewById(R.id.tv_total);
        tv_recommed_skuItemCode = findViewById(R.id.tv_recommed_skuItemCode);

        iv_order_select = findViewById(R.id.iv_order_select);
        mListView = findViewById(R.id.list_view);
        mBtnConfirm = findViewById(R.id.btn_finish);
        customHScrollView = findViewById(R.id.h_scrollView);

        iv_order_select.setOnClickListener(this);
        mBtnConfirm.setOnClickListener(this);
        MyTouchLinstener myTouchLinstener = new MyTouchLinstener(customHScrollView);
        mListView.setOnTouchListener(myTouchLinstener);

        setEditTextScannerEvents(et_replenishOrderNo, this);
        setEditTextScannerEvents(et_targetLocationcode, this);
        setEditTextScannerEvents(et_skuCode, this);
        setEditTextScannerEvents(et_fromLocationCode, this);
        setEditTextScannerEvents(et_replenish_count, this);
        et_replenishOrderNo.setOnFocusChangeListener(this);

        mAdapter = new InnerReplenishSkuListAdapter(this, this, customHScrollView);
        mListView.setAdapter(mAdapter);

        autoComplete = PreferencesUtils.getBoolean(this, "autoComplete");
        if (autoComplete) {
            mBtnConfirm.setVisibility(View.GONE);
        } else {
            mBtnConfirm.setVisibility(View.VISIBLE);
        }

        queryStockPresenter = new QueryStockPresenter(this, this);
    }

    @Override
    protected void initData() {
        super.initData();

        //通过billNo判断是否从补货详情页面进来，优先处理传进来的补货单
        billNo = getIntent().getStringExtra("billNo");
        if (!TextUtils.isEmpty(billNo)) {
            et_replenishOrderNo.setText(billNo);
            //触发输入事件
            et_replenishOrderNo.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER));
        } else {
            clearAllView();
        }
    }

    private void checkSkuInput(String input) {
        String skuSplit = CommonUtil.skuCodeSplit(InnerReplenishActivity.this, input);
        if (TextUtils.isEmpty(skuSplit)) {
            SpeakUtils.speakWithToast(InnerReplenishActivity.this, "商品条码截取结果为空，请检查输入和截图规则", false, false);
            return;
        }
        input = skuSplit;
        et_skuCode.setText(skuSplit);
        if (replenishSkuBeans.size() > 0) {
            boolean skuExit = false;
            for (ReplenishSkuBean bean : replenishSkuBeans) {
                if (bean.getItemBarCode().equals(input)) {
                    operatReplenishBean = bean;
                    skuExit = true;
                    isFocusToTargetLocation = false;
                    reflushSkuData();
                    break;
                }
            }
            if (!skuExit) {
                SpeakUtils.speakWithToast(this, input + "不属于本次所选补货任务！", false, false);
                operatReplenishBean = null;
                CommonUtil.requestFocusAndClearText(et_skuCode);
            } else {
                CommonUtil.requestFocusAndClearText(et_replenish_count);
            }
        } else {
            SpeakUtils.speakWithToast(this, "请选择/扫描补货单号！", false, false);
            et_skuCode.setText("");
            et_replenishOrderNo.requestFocus();
        }
    }

    //判断下一个推荐的sku、库位应该是哪个
    private void reflushNextSku() {
        et_skuCode.setText("");

        if (recommedPosition < replenishSkuBeans.size()) {
            operatReplenishBean = replenishSkuBeans.get(recommedPosition);
            if (!operatReplenishBean.isHasRecommed()) {//没有推荐并操作过刷新推荐，推荐过的话推荐下一个
                isFocusToTargetLocation = false;
                reflushSkuData();
                recommedPosition++;
            } else {
                recommedPosition++;
                reflushNextSku();
            }
        } else {
            //全部推荐完则清空部分界面
            tv_recommed_skuItemCode.setText("");
            tv_recommed_skuCode.setText("");
            tv_recommed_skuName.setText("");
            et_fromLocationCode.setText("");
            et_replenish_count.setText("");
            tv_replenish_detail.setText("");
            targetWarehouseLocation = null;
            fromWarehouseLocation = null;

            //重新查询补货单
            innerReplenishPresenter.queryReplenishOrderList("D,E", "", getCurrentWarehouse().getId(), 500, 1);
        }
    }

    //刷新推荐sku的界面
    private void reflushSkuData() {
        if (operatReplenishBean != null) {
            tv_recommed_skuCode.setText(operatReplenishBean.getItemCode());
            tv_recommed_skuItemCode.setText(operatReplenishBean.getItemBarCode());
            tv_recommed_skuName.setText(operatReplenishBean.getItemskuName());

            tv_replenish_detail.setText(operatReplenishBean.getWantQty() + "");
            et_fromLocationCode.setText(operatReplenishBean.getFromLocationNumber());
            //查询推荐来源库位基本信息
            queryLocationInfo(operatReplenishBean.getFromLocationNumber(), true);
        }
    }

    @Override
    public void onFocusChange(View view, boolean b) {

        if (view.getId() == et_replenishOrderNo.getId()) {
            if (b) {
                focusEditText = et_replenishOrderNo;
            }
            ll_replenish_order.setBackgroundResource(b ? R.drawable.shape_bg_edit_selected : R.drawable.dialog_edit_bg);
            return;
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            switch (v.getId()) {
                case R.id.et_replenishOrderNo:
                    billNo = input;
                    innerReplenishPresenter.queryReplenishOrderDetail(getCurrentWarehouse().getId(), billNo);
                    break;
                case R.id.et_targetLocationcode:
                    isFocusToTargetLocation = false;
                    queryLocationInfo(input, false);
                    break;
                case R.id.et_skuCode:
                    checkSkuInput(input);
                    break;
                case R.id.et_fromLocationCode:
                    isFocusToTargetLocation = false;
                    queryLocationInfo(input, true);
                    break;
                case R.id.et_replenish_count:
                    int count = CommonUtil.formatStringToInt(input);
                    //可移入量 = 库位的库容 - 库位库存数 - 该库位待移入数量
                    int canMoveInQty = targetWarehouseLocation.getCanMoveInQty();
                    //库容为0表示无限制
                    if (targetWarehouseLocation.getLocationLimit() == 0) {
                        canMoveInQty = Integer.MAX_VALUE;
                    }
                    if (targetWarehouseLocation == null) {
                        SpeakUtils.speakWithToast(this, "请先扫描/输入目标库位并点击PDA的“ENT”！", false, false);
                        et_targetLocationcode.requestFocus();
                    } else if (operatReplenishBean == null) {
                        SpeakUtils.speakWithToast(this, "请先扫描/输入正确的商品条码并点击PDA的“ENT”！", false, false);
                        et_targetLocationcode.requestFocus();
                    } else if (fromWarehouseLocation == null) {
                        SpeakUtils.speakWithToast(this, "请先输入来源库位并点击PDA的“ENT”！", false, false);
                        et_targetLocationcode.requestFocus();
                    } else if ((targetWarehouseLocation.getLocationId() + "").equals(fromWarehouseLocation.getLocationId() + "")) {
                        SpeakUtils.speakWithToast(this, "目标、来源库位不能一致", false, false);
                        et_fromLocationCode.requestFocus();
                    } else if (count <= 0) {
                        SpeakUtils.speakWithToast(this, "补货数量必须大于0！", false, false);
                    } else if (count > (fromWarehouseLocation.getCanMoveOutQty())) {
                        SpeakUtils.speakWithToast(this, "补货数量不可超过SKU在实际来源库位的库存！", false, false);
                    } else if (count > canMoveInQty) {
                        SpeakUtils.speakWithToast(this, "补货数量不可超过实际目的库位的剩余可容纳量！", false, false);
                    } else {
                        //刷新正在操作的数据
                        boolean haveDone = false;
                        a:
                        for (ReplenishSkuBean.ActulBean bean : operatReplenishBean.getActulBeans()) {
                            //如果此来源、目的库位已经操作过，则数量相加,跳出循环
                            if (bean.getTargetLocationNumber().equals(targetWarehouseLocation.getLocationCode()) && bean.getActualFromLocationNumber()
                                    .equals(fromWarehouseLocation.getLocationCode())) {
                                bean.setActulQty(bean.getActulQty() + count);
                                haveDone = true;
                                break a;
                            }
                        }
                        //没操作过则添加此条数据
                        if (!haveDone) {
                            List<ReplenishSkuBean.ActulBean> actulBeans = operatReplenishBean.getActulBeans();
                            ReplenishSkuBean.ActulBean actulBean = new ReplenishSkuBean.ActulBean(targetWarehouseLocation.getLocationCode(), targetWarehouseLocation
                                    .getLocationId(), fromWarehouseLocation.getLocationCode(), fromWarehouseLocation.getLocationId(), count);
                            actulBean.setItemBarCode(operatReplenishBean.getItemBarCode());
                            actulBean.setItemCode(operatReplenishBean.getItemCode());
                            actulBeans.add(actulBean);
                            operatReplenishBean.setActulBeans(actulBeans);
                        }

                        if (!autoComplete) {//如果不是自动提交模式
                            //输入数量后，此条数据标记为已推荐
                            operatReplenishBean.setHasRecommed(true);
                            mBtnConfirm.setEnabled(true);
                            mAdapter.notifyDataSetChanged();
                            et_replenish_count.setText("");
                            //每次输入完数量之后，判断是否需要刷新下一个推荐库位/条码
                            reflushNextSku();
                        } else {
                            try {
                                ArrayList<ReplenishCommitBean.DataBean> replenishCommitBeans = new ArrayList<>();
                                for (ReplenishSkuBean.ActulBean actulbean : operatReplenishBean.getActulBeans()) {
                                    if (operatReplenishBean.getRealQty() > 0) {
                                        ReplenishCommitBean.DataBean replenishCommitBean = new ReplenishCommitBean.DataBean(operatReplenishBean.getBillId(), operatReplenishBean.getItemskuId(),
                                                actulbean.getTargetLocationId(), actulbean.getActualFromLocationId(), actulbean.getActulQty(), operatReplenishBean.getInvStatusId());
                                        replenishCommitBeans.add(replenishCommitBean);
                                    }
                                }

                                ReplenishCommitBean replenishCommitBean = new ReplenishCommitBean();
                                replenishCommitBean.setData(replenishCommitBeans);
                                replenishCommitBean.setType("4");
                                innerReplenishPresenter.replenishOrderCommit(replenishCommitBean);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
            }
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_order_select:
                showAllReplenishOrderSelector();
                break;
            case R.id.btn_finish:
                orderCommit();
                break;
        }
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {
        clearAllView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void clearAllView() {
        et_replenishOrderNo.setText("");
        et_targetLocationcode.setText("");
        et_skuCode.setText("");
        tv_recommed_skuItemCode.setText("");
        tv_recommed_skuCode.setText("");
        tv_recommed_skuName.setText("");
        et_fromLocationCode.setText("");
        et_replenish_count.setText("");
        tv_replenish_detail.setText("");
        tv_total.setText("SKU种类数：-      总数：-");

        replenishSkuBeans.clear();
        targetWarehouseLocation = null;
        fromWarehouseLocation = null;
        billNo = "";

        //重新查询补货单
        innerReplenishPresenter.queryReplenishOrderList("D,E", "", getCurrentWarehouse().getId(), 500, 1);
    }

    /**
     * 展示补货单号选择框
     */
    private void showAllReplenishOrderSelector() {
        ListSelectorPpWindow<ReplenishOrderBean.DataBean> locationSelector = new ListSelectorPpWindow<>(InnerReplenishActivity.this,
                R.drawable.shape_bg_edit_selected,
                ll_replenish_order.getWidth(),
                orderList, new ListSelectorPpWindow.onItemSelectedListener() {
            @Override
            public void onItemSelected(Object item) {
                ReplenishOrderBean.DataBean dataBean = (ReplenishOrderBean.DataBean) item;
                et_replenishOrderNo.setText(dataBean.getBillNo());
                billNo = dataBean.getBillNo();
                innerReplenishPresenter.queryReplenishOrderDetail(getCurrentWarehouse().getId(), billNo);
            }
        });
        locationSelector.showAsDropDown(ll_replenish_order, 0, 10);
    }

    /**
     * 查询库位信息
     *
     * @param locationCode 库位编码
     * @param isMoveOut    是否为目的库位，用来区分光标聚焦到哪
     */
    private void queryLocationInfo(String locationCode, boolean isMoveOut) {
        this.isMoveOut = isMoveOut;
        String fromLocationCode = et_fromLocationCode.getText().toString().trim();
        String targetLocationCode = et_targetLocationcode.getText().toString().trim();
        if (!TextUtils.isEmpty(fromLocationCode) && !TextUtils.isEmpty(targetLocationCode) && fromLocationCode.equals(targetLocationCode)) {
            SpeakUtils.speakWithToast(this, "目的库位和来源库位不能一样！", false, false);
            if (!isMoveOut) {
                CommonUtil.requestFocusAndClearText(et_targetLocationcode);
            } else {
                CommonUtil.requestFocusAndClearText(et_fromLocationCode);
            }
            return;
        }

        queryStockPresenter.queryWarehouseLocation(getCurrentWarehouse().getId(), locationCode);
    }

    private void queryGoodsLocationStock(WarehouseLocation location) {
        queryStockPresenter.queryGoodsLocationStock(location, operatReplenishBean.getItemskuId(), 0);
    }

    /**
     * 补货单提交
     */
    protected void orderCommit() {
        try {
            ArrayList<ReplenishCommitBean.DataBean> replenishCommitBeans = new ArrayList<>();
            for (ReplenishSkuBean skuBean : replenishSkuBeans) {
                for (ReplenishSkuBean.ActulBean actulbean : skuBean.getActulBeans()) {
                    if (skuBean.getRealQty() > 0) {
                        ReplenishCommitBean.DataBean replenishCommitBean = new ReplenishCommitBean.DataBean(replenishSkuBeans.get(0).getBillId(), skuBean.getItemskuId(),
                                actulbean.getTargetLocationId(), actulbean.getActualFromLocationId(), actulbean.getActulQty(), skuBean.getInvStatusId());
                        replenishCommitBeans.add(replenishCommitBean);
                    }
                }
            }

            ReplenishCommitBean replenishCommitBean = new ReplenishCommitBean();
            replenishCommitBean.setData(replenishCommitBeans);
            replenishCommitBean.setType("4");
            innerReplenishPresenter.replenishOrderCommit(replenishCommitBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetOrderSuccess(List<ReplenishOrderBean.DataBean> list) {
        orderList.clear();
        orderList.addAll(list);
        CommonUtil.requestFocusAndNoClearText(et_replenishOrderNo);
    }

    @Override
    public void onGetOrderFailed(String msg) {
        SpeakUtils.speakWithToast(this, msg, false, false);
    }

    @Override
    public void queryReplenishOderDetailSuccess(ReplenishOrderDetailBean.DataBean dataBean) {
        try {
            if ("D".equals(dataBean.getListVo().getBillStatus()) || "E".equals(dataBean.getListVo().getBillStatus())) {
                replenishSkuBeans.clear();
                //后台所有数据一起给的，所以我们这里过滤整合成便于操作的自定义对象
                //以推荐来源库位为纬度展示sku列表
                int totalNum = 0;
                HashSet<String> skus = new HashSet<>();
                List<ReplenishOrderDetailBean.DataBean.RepsBean> reps = dataBean.getReps();
                //遍历所有推荐库位信息
                for (ReplenishOrderDetailBean.DataBean.AllotsBean allotsBean : dataBean.getAllots()) {
                    //遍历所有sku信息
                    for (ReplenishOrderDetailBean.DataBean.ItemsBean bean : dataBean.getItems()) {
                        if (bean.getItemskuId() == allotsBean.getItemskuId()) {
                            ReplenishSkuBean replenishSkuBean = new ReplenishSkuBean();
                            replenishSkuBean.setBillId(dataBean.getListVo().getBillId());
                            replenishSkuBean.setItemskuId(bean.getItemskuId());
                            replenishSkuBean.setItemName(bean.getItemName());
                            replenishSkuBean.setItemskuCode(bean.getItemskuCode());
                            replenishSkuBean.setItemCode(bean.getItemCode());
                            replenishSkuBean.setItemBarCode(bean.getItemBarCode());
                            replenishSkuBean.setItemskuName(bean.getItemskuName());
                            replenishSkuBean.setInvStatusId(bean.getInvStatusId());
                            replenishSkuBean.setInvStatusName(bean.getInvStatusName());
                            replenishSkuBean.setWarehouseAreaId(allotsBean.getWarehouseAreaId());
                            replenishSkuBean.setWarehouseAreaName(allotsBean.getWarehouseAreaName());
                            replenishSkuBean.setFromLocationNumber(allotsBean.getLocationCode());
                            replenishSkuBean.setFromLocationId(allotsBean.getLocationId());
                            replenishSkuBean.setWantQty(allotsBean.getQty());
                            replenishSkuBean.setRealQty(0);

                            boolean isShow = true;
                            //遍历已补货信息
                            for (int i = reps.size() - 1; i >= 0; i--) {
                                ReplenishOrderDetailBean.DataBean.RepsBean repsBean = reps.get(i);
                                //如果这个sku已补，则不显示在补货列表
                                if (allotsBean.getItemskuId() == repsBean.getItemskuId()) {
                                    isShow = false;
                                    //获取完数据删除，防止同一skuid分配了两个来源库位的情况（虽然很少出现）
                                    reps.remove(i);
                                }
                            }

                            //如果此sku已补，不再展示
                            if (isShow) {
                                replenishSkuBeans.add(replenishSkuBean);
                                totalNum += bean.getWantQty();
                                skus.add(bean.getItemskuCode());
                            }

                            break;
                        }
                    }
                }

                Collections.sort(replenishSkuBeans, new Comparator<ReplenishSkuBean>() {
                    public int compare(ReplenishSkuBean e1, ReplenishSkuBean e2) {
                        int a = e1.getFromLocationNumber().compareTo(e2.getFromLocationNumber());
                        if (a != 0) {
                            return a > 0 ? 1 : -1;
                        }
                        a = e1.getItemBarCode().compareTo(e2.getItemBarCode());
                        return a > 0 ? 1 : -1;
                    }
                });

                et_targetLocationcode.requestFocus();

                //查询到补货单详情后，默认取第一条推荐
                operatReplenishBean = replenishSkuBeans.get(0);
                tv_total.setText("SKU种类数：" + skus.size() + "       总数：" + totalNum);
                //刷新下方SKU列表
                mAdapter.setData(replenishSkuBeans);
                isFocusToTargetLocation = true;
                reflushSkuData();
            } else {
                SpeakUtils.speakWithToast(InnerReplenishActivity.this, "补货单非可补货状态", false, false);
                clearAllView();
            }
        } catch (Exception e) {
            e.printStackTrace();
            SpeakUtils.speakWithToast(InnerReplenishActivity.this, "此补货单已完成！", false, true);
            clearAllView();
        }
    }

    @Override
    public void queryWarehouseLocationSuccess(WarehouseLocation warehouseLocation) {
        queryGoodsLocationStock(warehouseLocation);
    }

    @Override
    public void queryWarehouseLocationFailed(String message) {
        SpeakUtils.speakWithToast(InnerReplenishActivity.this, message, false, false);
        if (!isMoveOut) {
            et_targetLocationcode.setText("");
            et_targetLocationcode.requestFocus();
        } else {
            et_fromLocationCode.setText("");
            et_fromLocationCode.requestFocus();
        }
        //清空库位信息
        targetWarehouseLocation = null;
        fromWarehouseLocation = null;
    }

    @Override
    public void queryGoodsLocationStockSuccess(WarehouseLocation warehouseLocation) {
        //保存来源、目标库位数据信息
        if (!isMoveOut) {
            targetWarehouseLocation = warehouseLocation;
        } else {
            fromWarehouseLocation = warehouseLocation;
            //匹配来源库位的库存状态
            for (LocationListBean bean : fromWarehouseLocation.getList()) {
                if (operatReplenishBean != null && bean.getInvStatusId() == operatReplenishBean.getInvStatusId()) {
                    fromWarehouseLocation.setSelectedStatusBean(bean);
                }
            }
        }
        et_replenish_count.setText("");
        if (isFocusToTargetLocation) {
            et_targetLocationcode.requestFocus();
        } else if (et_skuCode.isFocused()) {
            et_replenish_count.requestFocus();
        } else if (TextUtils.isEmpty(et_skuCode.getText().toString())) {
            et_skuCode.requestFocus();
        } else {
            et_replenish_count.requestFocus();
        }
    }

    @Override
    public void queryGoodsLocationStockFailed(String message) {
        SpeakUtils.speakWithToast(InnerReplenishActivity.this, message, false, false);
        if (isMoveOut) {
            et_targetLocationcode.setText("");
            et_targetLocationcode.requestFocus();
        } else {
            et_fromLocationCode.setText("");
            et_fromLocationCode.requestFocus();
        }
        //清空库位信息
        targetWarehouseLocation = null;
        fromWarehouseLocation = null;
    }

    @Override
    public void queryReplenishOderDetailFailed(String msg) {
        SpeakUtils.speakWithToast(this, msg, false, false);
    }

    @Override
    public void replenishOrderCommitSuccess(String msg) {
        SpeakUtils.speakWithToast(this, msg, false, true);
        if (autoComplete) {//如果是自动提交模式
            operatReplenishBean.setHasRecommed(true);
            mAdapter.notifyDataSetChanged();
            et_replenish_count.setText("");
            //每次输入完数量之后，判断是否需要刷新下一个推荐库位/条码
            reflushNextSku();
        } else {
            clearAllView();
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void replenishOrderCommitFailed(String msg) {
        SpeakUtils.speakWithToast(this, msg, false, false);
    }

    @Override
    public void onSkuItemClick(int position) {
        operatReplenishBean = replenishSkuBeans.get(position);
        isFocusToTargetLocation = false;
        et_skuCode.setText("");
        reflushSkuData();
    }
}
